﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enemies;
using MapLibrary.Players;
using Animation;
using Microsoft.Xna.Framework;

namespace MapLibrary.Particles.Emitters
{
    class ProteinEmitter : TriggeredParticleEmitter
    {

        //has to be 3 element array
        private int[] proteinAmounts;
        private Sprite sprite;

        public ProteinEmitter(ParticleManager manager, Sprite s, int[] proteinAmounts) : base(manager)
        {
            sprite = s;
            this.proteinAmounts = proteinAmounts;
        }

        protected override void Emit()
        {
            ParticleInitialConditions conds;

            List<ParticleInitialConditions> condList = new List<ParticleInitialConditions>();
            for (int i = 0; i < numberOfProteinParticles(); i++)
            {
                Vector2 offset = new Vector2(GameWorld.Random.Next(0, 100), GameWorld.Random.Next(0, 100));
                 conds = new ParticleInitialConditions();
                 conds.Type = "Protein";
                 conds.Position = sprite.Body.Position + (offset/100);
                 conds.Lifetime = ProteinParticle.PROTEIN_LIFETIME;
                 conds.Velocity = new Microsoft.Xna.Framework.Vector2(-1, -2);
                 conds.Kinematic = false;
                 conds.AffectedByGravity = false;

                 condList.Add(conds);
            }

            List<Particle> particles = particleManager.GetFromInitialConditions(condList);

            int proteinIndex = 0;
            for (int i = 0; i < 3; i++)
            {
                int toIterate = numberOfProteinType(i);

                for (int j = 0; j < toIterate; j++)
                {
                    ProteinParticle p = (ProteinParticle)particles[proteinIndex];

                    if (j != toIterate - 1)
                        p.AmountOfProtein = 10;
                    else
                        p.AmountOfProtein = proteinAmounts[i] % 10;

                    p.TypeOfProtein = (ProteinParticle.ProteinType)i;
                    proteinIndex++;
                }
            }
        }

        private int numberOfProteinParticles()
        {
            int totalProtein = 0;

            for (int i = 0; i < proteinAmounts.Length; i++)
                totalProtein += proteinAmounts[i];

            return totalProtein / 10;   //10 protein per particle
        }

        private int numberOfProteinType(int type)
        {
            return proteinAmounts[type] / 10;
        }
    }
}
